﻿#region [ Code Modification History ]

//*******************************************************************************************************
//  ByteBlockWriter.cs 
//
//  Code Modification History:
//  -----------------------------------------------------------------------------------------------------
//  15/01/2017 - V. Dennis Kholin
//       Generated original version of source code. 
//*******************************************************************************************************

#endregion

#region [ Using ]

using System;
using System.IO;
using GZipStreamTool.Base.Gzip;

#endregion

namespace GZipStreamTool.Base.Core
{

    #region [ Delegates ]

    /// <summary>
    ///     ByteBlockWriterDelegate
    /// </summary>
    /// <param name="e"></param>
    /// <param name="o"></param>
    public delegate void ByteBlockWriterDelegate(object e, object o);

    #endregion

    #region [ Enumeractions ]

    /// <summary>
    ///     ActionType
    /// </summary>
    public enum ActionType
    {
        /// <summary>
        ///     Compress
        /// </summary>
        Compress,

        /// <summary>
        ///     Decompress
        /// </summary>
        Decompress,

        /// <summary>
        ///     Cancel
        /// </summary>
        Cancel
    }

    #endregion

    /// <summary>
    ///     ByteBlockWriter
    /// </summary>
    public class ByteBlockWriter : IDisposable
    {
        #region [ Constructors ]

        #endregion

        #region [ Disposing ]

        /// <summary>
        ///     Dispose
        /// </summary>
        public void Dispose()
        {
            WriterFileStream.Close();
            WriterFileStream.Dispose();

            WriterFileStream?.Dispose();
        }

        #endregion

        #region [ Members ]

        //    private static int BUFFER_SIZE = 64 * 1024;

        #endregion

        #region [ Properties ]

        /// <summary>
        ///     Action
        /// </summary>
        public ActionType Action { get; set; }

        /// <summary>
        ///     FileToCompress
        /// </summary>
        public FileStream WriterFileStream { get; set; }

        #endregion

        #region [ Methods ]

        /// <summary>
        ///     Write
        /// </summary>
        /// <param name="b"></param>
        public void Write(byte[] b)
        {
            WriterFileStream.Write(b, 0, b.Length);
        }

        /// <summary>
        ///     Write
        /// </summary>
        /// <param name="gzb"></param>
        public void Write(GZipBlock gzb)
        {
            using (WriterFileStream)
            {
                WriterFileStream.Write(gzb.BlockOut, 0, gzb.BlockOut.Length);
            }
        }

        private void _bbr_BlockReadout(object args, object obj)
        {
            GZipTools.GZipStreamTool.Compress(((ByteBlock) args).Block);
        }

        #endregion
    }
}